home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / sml_nj / cml-098.lha / cml-0.9.8 / library / cobegin.sml < prev    next >
Encoding:
Text File  |  1991-06-16  |  847 b   |  36 lines

  1. (* cobegin
  2.  *
  3.  * COPYRIGHT (c) 1990 by John H. Reppy.  See COPYRIGHT file for details.
  4.  *)
  5.  
  6. signature COBEGIN = sig
  7.     structure CML : CONCUR_ML
  8.     val cobegin : (unit -> unit) list -> unit CML.event
  9.   end (* COBEGIN *)
  10.  
  11. functor Cobegin (CML : CONCUR_ML) : COBEGIN =
  12.   struct
  13.     structure CML : CONCUR_ML = CML
  14.  
  15.     fun cobegin fl = let
  16.       fun spawn ([], l) = l
  17.         | spawn (p::r, l) = let
  18.         val id = CML.spawn p
  19.         in
  20.           spawn(r, (id, CML.wrap (CML.threadWait id, fn () => id))::l)
  21.         end
  22.       val extract = map (fn (_, evt) => evt)
  23.       fun barrier [] = ()
  24.         | barrier l = let
  25.         val id = CML.select (extract l)
  26.         fun f ((x as (id', evt)) :: r) =
  27.               if CML.sameThread(id, id') then r else (x :: (f r))
  28.         in
  29.           barrier (f l)
  30.         end
  31.       in
  32.         CML.threadWait (CML.spawn (fn () => barrier(spawn(fl, []))))
  33.       end
  34.  
  35.   end (* functor Cobegin *)
  36.